﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Xml;

namespace RssReader
{
    public class RssReader
    {
        #region Properties

        /// <summary>
        /// Gets or sets the URL of the RSS feed to parse.
        /// </summary>
        public string Url { get; set; }

        /// <summary>
        /// Gets all the items in the RSS feed.
        /// </summary>
        public IEnumerable<Rss> RssItems
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets the title of the RSS feed.
        /// </summary>
        public string FeedTitle
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets the description of the RSS feed.
        /// </summary>
        public string FeedDescription
        {
            get;
            private set;
        }

        #endregion

        #region Constructors

        public RssReader()
        {
            RssItems = new List<Rss>();
        }

        public RssReader(string feedUrl)
            : this()
        {
            this.Url = feedUrl;
        }

        #endregion

        #region Methods

        /// <summary>
        /// Retrieves the remote RSS feed and parses it.
        /// </summary>
        public void DownloadFeed()
        {
            if (String.IsNullOrEmpty(Url))
            {
                throw new ArgumentException("You must provide a feed URL");
            }

            using (XmlReader reader = XmlReader.Create(Url))
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(reader);
                FeedTitle = ParseRssElements(xmlDoc.SelectSingleNode("//channel"), "title");
                FeedDescription = ParseRssElements(xmlDoc.SelectSingleNode("//channel"), "description");
                ParseRssItems(xmlDoc);
            }
        }

        /// <summary>
        /// Parses the xml document in order to retrieve the RSS items.
        /// </summary>
        private void ParseRssItems(XmlDocument xmlDoc)
        {
            RssItems = new List<Rss>();
            XmlNodeList items = xmlDoc.SelectNodes("rss/channel/item");

            foreach (XmlNode item in items)
            {
                Rss rss = new Rss
                    {
                        Title = ParseRssElements(item, "title"),
                        Description = ParseRssElements(item, "description"),
                        Link = ParseRssElements(item, "link"),
                        Date = DateTime.Parse(ParseRssElements(item, "pubDate"))
                    };

                (RssItems as List<Rss>).Add(rss);
            }
        }

        /// <summary>
        /// Parses the XmlNode element.
        /// </summary>
        private string ParseRssElements(XmlNode parent, string xPath)
        {
            XmlNode node = parent.SelectSingleNode(xPath);
            if (node != null)
            {
                return node.InnerText;
            }
            else
            {
                return string.Empty;
            }
        }

        #endregion
    }
}
